package com.example.nftapp.frg.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.nftapp.R;
import com.example.nftapp.adapter.CommonPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private final List<Fragment> fragments = new ArrayList<>();
    private final List<String> tabs = new ArrayList<>();
    private CommonPagerAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragments.add(new HomeFragment_TJ());
        fragments.add(new HomeFragment_ZT());
        tabs.add("推荐");
        tabs.add("专题");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewPager2 vpHome = view.findViewById(R.id.vpHome);
        TabLayout tabsHome = view.findViewById(R.id.tabsHome);
        adapter = new CommonPagerAdapter(requireActivity(), fragments);
        vpHome.setAdapter(adapter);
        new TabLayoutMediator(tabsHome, vpHome, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                if (adapter != null) {
                    tab.setText(tabs.get(position));
                }
            }
        }).attach();
    }

}